<template>
  <div>
    <el-form ref="form" :model="FEditAdmin" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="登陆账号">
            <el-input
              v-model="FEditAdmin.FAdminCode"
              placeholder="请输入登陆账号,一旦保存不可改动"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="登陆密码"
            placeholder="请输入登陆密码,不低于6位数"
          >
            <el-input v-model="FEditAdmin.FAdminPass"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="用户名称">
            <el-input v-model="FEditAdmin.FAdminName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话">
            <el-input v-model="FEditAdmin.FAdminTel"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="用户生日">
            <el-input v-model="FEditAdmin.FAdminBirthday"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户性别">
            <el-input v-model="FEditAdmin.FAdminSex"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="所属组织">
        <el-cascader
          v-model="FEditAdmin.tempCompanyID"
          :options="FCompanyTree"
          :props="{ checkStrictly: true }"
          filterable
          clearable
          placeholder="单击选择"
          size="medium"
        ></el-cascader>
      </el-form-item>

      <el-row>
        <el-col :span="12">
          <el-form-item label="所属角色">
            <el-cascader
              v-model="FEditAdmin.tempRoleID"
              :options="FRoleTree"
              :props="{ checkStrictly: true }"
              filterable
              clearable
              placeholder="单击选择"
              size="medium"
            ></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item>
            <el-switch
              v-model="FEditAdmin.FStatusCode"
              active-text="启用"
              inactive-text="禁用"
              active-value="启用"
              inactive-value="禁用"
            >
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item>
            <el-checkbox v-model="FEditAdmin.FIsAdmin">管理员</el-checkbox>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="用户邮箱">
            <el-input v-model="FEditAdmin.FAdminMail"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注">
            <el-input v-model="FEditAdmin.FRemark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row justify="end">
      <el-button type="primary" @click="onSave" :disabled="!zIsOpenData"
        >保存</el-button
      >
      <el-button type="warning" @click="onCancel">取消</el-button>
    </el-row>
  </div>
</template>

<script  lang="ts">
import {
  TBaseAdmin,
  TBaseCompany,
  TBaseCompanyClass,
  TBaseRole,
} from "@/baselib/right/BaseRightClass";
import { Options } from "vue-class-component";
import TFormBase from "@/views/BaseForm";
import elmHelper, { IElmTreeData } from "../../model/elmHelper";
//配合着用
@Options({
  name: "TFormAdminEdit",
  components: {},
  computed: {},
  data() {},
})
export default class TFormAdminEdit extends TFormBase {
  private FEditAdmin: TBaseAdmin = new TBaseAdmin();
  private FCompanyList: TBaseCompany[] = [];
  private FCompanyTree: IElmTreeData[] = [];
  private FRoleList: TBaseRole[] = [];
  private FRoleTree: IElmTreeData[] = [];
  //
  private FClassList: TBaseCompanyClass[] = [];
  private FClassElmDataTree: IElmTreeData[] = [];
  public async created() {
    this.FEditAdmin = this.zContextParams.IndexAdmin;
    this.FEditAdmin.tempRoleID = this.FEditAdmin.FRoleID;
    this.FEditAdmin.tempCompanyID = this.FEditAdmin.FCompanyID;
    this.openCompanyList();
    this.openRoleList();
    this.openData();
  }
  //打开数据
  public async openData() {
    this.zIsOpenData = false;
    if (!this.$myStrHelper.stringIsEmptyYW(this.FEditAdmin.FAdminID)) {
      let lResult = await this.$myBaseRightAPI.getAdmin(
        this.FEditAdmin.FAdminID
      );
      if (!lResult.resultSuccess) {
        this.$myMsgHelp.msgDialog(lResult.resultMsg);
        return;
      }
      if (this.$myStrHelper.stringIsEmptyYW(this.FEditAdmin.FAdminID)) {
        this.$myMsgHelp.msgDialog(
          "返回的数据结构有问题:" + JSON.stringify(lResult.resultData)
        );
        return;
      }
      this.FEditAdmin = lResult.resultData;
      this.FEditAdmin.tempRoleID = this.FEditAdmin.FRoleID;
      this.FEditAdmin.tempCompanyID = this.FEditAdmin.FCompanyID;
       this.zIsOpenData = true;
    } else {
      this.zIsOpenData = true;
    }
  }
  public async openCompanyList() {
    let lResult = await this.$myBaseRightAPI.getCompanyList();
    if (lResult.resultSuccess) {
      this.FCompanyList = lResult.resultData;
      this.FCompanyTree = elmHelper.dataTreeToElmTree(
        this.FCompanyList,
        "FCompanyID",
        "children",
        "FCompanyName",
        "FCompanyID"
      );
    }
  }
  public async openRoleList() {
    let lResult = await this.$myBaseRightAPI.getRoleList();
    if (lResult.resultSuccess) {
      this.FRoleList = lResult.resultData;
      this.FRoleTree = elmHelper.dataTreeToElmTree(
        this.FRoleList,
        "FRoleID",
        "children",
        "FRoleName",
        "FRoleID"
      );
    }
  }
  //
  private async onSave() {
    //当清空是elm返回的是null,多级返回的是数组
    if (this.FEditAdmin.tempCompanyID == null) {
      this.FEditAdmin.FCompanyID = "";
    } else if (this.FEditAdmin.tempCompanyID instanceof Array) {
      let temp =
        this.FEditAdmin.tempCompanyID[this.FEditAdmin.tempCompanyID.length - 1];
      this.FEditAdmin.FCompanyID = temp;
    }
    //
    if (this.FEditAdmin.tempRoleID == null) {
      this.FEditAdmin.FRoleID = "";
    } else if (this.FEditAdmin.tempRoleID instanceof Array) {
      let temp =
        this.FEditAdmin.tempRoleID[this.FEditAdmin.tempRoleID.length - 1];
      this.FEditAdmin.FRoleID = temp;
    }
    //保存数据
    let lResult = await this.$myBaseRightAPI.saveAdmin(this.FEditAdmin);
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    this.$myMsgHelp.msgHintAutoClose("保存数据成功");
    if (this.zContextOnCall != null && this.zContextOnCall != undefined) {
      this.zContextOnCall("save", this.FEditAdmin);
    }
  }
  private onCancel() {
    if (this.zContextOnCall != null && this.zContextOnCall != undefined) {
      this.zContextOnCall("close", this.FEditAdmin);
    }
  }
}
</script>